import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    meta:{
      requireAuth:true
    },
    component: Home
  }
]

const router = new VueRouter({
  routes
})
// 全局路由守卫
router.beforeEach((to, from, next) => {
  // console.log('to',to);
  // console.log('from',from);
  // console.log('next',next);
  next();  
  // 判断该路由是否需要登录权限 因为在没有token的时候除了登录页面
  //不用拦截其他都要拦截所以判断要去取反  
  if (to.meta.requireAuth) { 
    if(JSON.parse(localStorage.getItem('token'))!=null){ //判断本地是否存在access_token
      next();
    }else {
      next({
        path:'/'
      })
    }
  }
  else {
   
  }
});
export default router
